package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Topic;
import com.ruoyi.system.service.ITopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Pool;
import com.ruoyi.system.service.IPoolService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 题库管理Controller
 * 
 * @author ruoyi
 * @date 2023-01-12
 */
@Controller
@RequestMapping("/system/pool")
public class PoolController extends BaseController
{
    private String prefix = "system/pool";

    @Autowired
    private IPoolService poolService;

    @Autowired
    private ITopicService topicService;

    @Log(title = "题库管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:pool:import")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        String filename = file.getOriginalFilename();
        if (poolService.selectPoolName().contains(filename)){
            return AjaxResult.error("存在同名题库");
        }else {
            ExcelUtil<Topic> util = new ExcelUtil<Topic>(Topic.class);
            List<Topic> topicList = util.importExcel(file.getInputStream());
            Pool pool = new Pool();
            pool.setPoolName(filename.split("\\.")[0]);
            pool.setTopicList(topicList);
            poolService.insertPool(pool);
            String message = topicService.importTopic(pool);
            return AjaxResult.success(String.valueOf(message));
        }
    }

    @RequiresPermissions("system:pool:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Topic> util = new ExcelUtil<Topic>(Topic.class);
        return util.importTemplateExcel("题库名");
    }


    @RequiresPermissions("system:pool:view")
    @GetMapping()
    public String pool()
    {
        return prefix + "/pool";
    }

    /**
     * 查询题库管理列表
     */
    @RequiresPermissions("system:pool:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Pool pool)
    {
        startPage();
        List<Pool> list = poolService.selectPoolList(pool);
        return getDataTable(list);
    }

    /**
     * 导出题库管理列表
     */
    @RequiresPermissions("system:pool:export")
    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Pool pool)
    {
        Topic topic = new Topic();
        topic.setPoolId(pool.getPoolId());
        List<Topic> list = topicService.selectTopicList(topic);
        ExcelUtil<Topic> util = new ExcelUtil<>(Topic.class);
        return util.exportExcel(list, pool.getPoolName());
    }

    /**
     * 新增题库管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存题库管理
     */
    @RequiresPermissions("system:pool:add")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Pool pool)
    {
        if (poolService.selectPoolName().contains(pool.getPoolName())){
            return AjaxResult.error("存在同名题库");
        }
        return toAjax(poolService.insertPoolByPrimaryChildTable(pool));
    }

    /**
     * 修改题库管理
     */
    @RequiresPermissions("system:pool:edit")
    @GetMapping("/edit/{poolId}")
    public String edit(@PathVariable("poolId") Long poolId, ModelMap mmap)
    {
        Pool pool = poolService.selectPoolByPoolId(poolId);
        mmap.put("pool", pool);
        return prefix + "/edit";
    }

    /**
     * 修改保存题库管理
     */
    @RequiresPermissions("system:pool:edit")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Pool pool)
    {
        if (!poolService.selectPoolByPoolId(pool.getPoolId()).getPoolName().equals(pool.getPoolName()) && poolService.selectPoolName().contains(pool.getPoolName())){
            return AjaxResult.error("存在同名题库");
        }
        return toAjax(poolService.updatePool(pool));
    }

    /**
     * 展示状态修改
     */
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:pool:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Pool pool)
    {
        return toAjax(poolService.updatePool(pool));
    }

    /**
     * 删除题库管理
     */
    @RequiresPermissions("system:pool:remove")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(poolService.deletePoolByPoolIds(ids));
    }
}
